package com.example.demo.controller;

import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @Author: yucsa
 * @Date: 2021/8/22 21:39
 * 控制账户登录、登出
 * 该文件被吞了，补一次
 */

@Controller
public class AccountController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;


    @RequestMapping("/account/toReg")
    public String toReg(){
        return "register";
    }

    @RequestMapping("/account/toLogin")
    public String toLogin(Model model) {
        return "login";
    }

    /**
     * @yucs 2021/8/27
     */
    @ApiOperation("退出登录的映射，调用service层的logout")
    @RequestMapping(value = "/account/logout",method = RequestMethod.GET)
    public String logout() {
        return userService.logout();
    }

    @ApiOperation("接收用户输入的用户名和密码，做注册")
    @RequestMapping(value = "/account/register",method = RequestMethod.POST)
    public String register(@RequestParam("username") String username, @RequestParam("password") String password){
        String target = userService.register(username,password);
        return target;
    }

    @ApiOperation("接收用户输入做登录")
    @RequestMapping(value = "/account/login",method = RequestMethod.POST)
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("type") String type, HttpSession httpSession){
        return userService.login(username,password,type,httpSession);
    }

}
